﻿#include "jsoncommentxls.h"
#include <QJsonParseError>
#include <QJsonDocument>
#include <QMessageBox>
#include <QJsonObject>
#include <QTableWidgetItem>
#include <QComboBox>
#include <QCheckBox>
#include <QJsonArray>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QTextStream>
#include "QHighlighter.h"
#if defined(_MSC_VER) && (_MSC_VER >= 1600)  
# pragma execution_character_set("utf-8")  
#endif 

JsonCommentXls::JsonCommentXls(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);

	Qt::WindowFlags windowFlag  = Qt::Dialog;
	windowFlag                  |= Qt::WindowMinimizeButtonHint;
	windowFlag                  |= Qt::WindowMaximizeButtonHint;
	windowFlag                  |= Qt::WindowCloseButtonHint;

	setWindowFlags(windowFlag);
	
	connect(ui.pushButton_Convert, SIGNAL(clicked()), this, SLOT(sltConvert()));
	connect(ui.pushButton_Save, SIGNAL(clicked()), this, SLOT(sltMarkdown()));

	ui.tableWidget->horizontalHeader()->setStretchLastSection(true); 

	new QHighlighter(ui.textEdit_Json->document());
	new QHighlighter(ui.textEdit_Markdown->document());
}

JsonCommentXls::~JsonCommentXls()
{

}

void JsonCommentXls::InsertValue(QString key, QJsonValueRef& value, int& nLevel)
{
	if (value.isNull())
	{
		return;
	}
	if (key.isEmpty() == false)
	{
		QString strName;
		for (int i=0; i<nLevel; i++)
		{
			strName += "+";
		}
		strName += key;

		int iRow = ui.tableWidget->rowCount();
		ui.tableWidget->setRowCount(iRow + 1);
		QTableWidgetItem *item = new QTableWidgetItem(strName);
		ui.tableWidget->setItem(iRow, 0, item);
		item->setFlags(item->flags() & (~Qt::ItemIsEditable));

		if (value.isArray())
		{
			QTableWidgetItem *item = new QTableWidgetItem("array[]");
			ui.tableWidget->setItem(iRow, 1, item);
			item->setFlags(item->flags() & (~Qt::ItemIsEditable));
		}
		else if (value.isObject())
		{
			QTableWidgetItem *item = new QTableWidgetItem("object");
			ui.tableWidget->setItem(iRow, 1, item);
			item->setFlags(item->flags() & (~Qt::ItemIsEditable));
		}	
		else
		{
			QComboBox* comboBox_Type = new QComboBox();
			comboBox_Type->insertItem(0, "string");
// 			comboBox_Type->insertItem(1, "object");
// 			comboBox_Type->insertItem(2, "array[]");
			comboBox_Type->insertItem(1, "number");
			comboBox_Type->insertItem(2, "bool");

			comboBox_Type->setProperty("row", iRow);
			comboBox_Type->setProperty("col", 1);
			comboBox_Type->setCurrentIndex(0);
			ui.tableWidget->setCellWidget(iRow, 1, comboBox_Type);
		}
		//connect(comboBox_Type, SIGNAL(currentIndexChanged(int)), this, SLOT(sltChangedType(int)));

		QCheckBox* checkbox = new QCheckBox();
		checkbox->setChecked(true);
		QWidget *w = new QWidget(this);
		QHBoxLayout *hLayout = new QHBoxLayout();
		hLayout->addWidget(checkbox);
		hLayout->setMargin(0);
		hLayout->setAlignment(checkbox, Qt::AlignCenter);
		w->setLayout(hLayout);

		checkbox->setProperty("row", iRow);
		checkbox->setProperty("col", 2);
		ui.tableWidget->setCellWidget(iRow, 2, w);

		m_mapCheckBox[iRow] = checkbox;
		item = new QTableWidgetItem("");
		ui.tableWidget->setItem(iRow, 3, item);

	}

	if (value.isArray())
	{
		QJsonArray array = value.toArray();
		if (array.size() > 0)
		{
			QJsonValueRef value = array[0];
			if (value.isObject() || value.isArray())
			{
				//nLevel++;
				InsertValue("", value, nLevel);
				//nLevel--;
			}
		}
	}
	else if (value.isObject())
	{
		QJsonObject object = value.toObject();
		QStringList lst = object.keys();

		QJsonObject::iterator it = object.begin();
		while(it != object.end())
		{
			if (!(it.value().isObject() || it.value().isArray()))
			{
				nLevel++;
				InsertValue(it.key(), it.value(), nLevel);
				nLevel--;
			}
			it++;
		}

		it = object.begin();
		while(it != object.end())
		{
			if ((it.value().isObject() || it.value().isArray()))
			{
				nLevel++;
				InsertValue(it.key(), it.value(), nLevel);
				nLevel--;
			}
			it++;
		}
	}
}

void JsonCommentXls::sltConvert()
{
	QString strJson = ui.textEdit_Json->toPlainText();
	for(int i = ui.tableWidget->rowCount();i > 0;i--)
	{
		ui.tableWidget->removeRow(0);
	}
	QJsonParseError jsonerror;
	QJsonDocument doc = QJsonDocument::fromJson(strJson.toLatin1(), &jsonerror);
	if (doc.isNull() || jsonerror.error != QJsonParseError::NoError)
	{
		QMessageBox::warning(this, "error", "parse json text error!");
		return;
	}

	if(doc.isObject())
	{
		int nLevel = 0;
		QJsonObject object = doc.object();

		QStringList lst = object.keys();
		QJsonObject::iterator it = object.begin();
		while(it != object.end())
		{
			if (!(it.value().isObject() || it.value().isArray()))
			{
				InsertValue(it.key(), it.value(), nLevel);
			}
			it++;
		}

		it = object.begin();
		while(it != object.end())
		{
			if ((it.value().isObject() || it.value().isArray()))
			{
				InsertValue(it.key(), it.value(), nLevel);
			}
			it++;
		}
	}

}

void JsonCommentXls::sltMarkdown()
{
	QString str;
	str += "| 字段 | 类型 | 必选 | 说明 |";
	str += "\n";

	str += "| ---- | ---- | ---- | ---- |";
	str += "\n";

	//正文
	int iRow = ui.tableWidget->rowCount();
	for (int i=0; i<iRow; i++)
	{
		QStringList ItemRows;
		for (int j=0; j<4; j++)
		{
			QTableWidgetItem* item = ui.tableWidget->item(i, j);
			if (j == 0)
			{
				ItemRows.append(item->text());
			}
			else if (j == 1)
			{
				QComboBox* comboBox_Type = (QComboBox*)ui.tableWidget->cellWidget(i, j);
				if (comboBox_Type)
				{
					ItemRows.append(comboBox_Type->currentText());
				}
				else
				{
					ItemRows.append(item->text());
				}
			}
			else if (j == 2)
			{
				QCheckBox* checkbox = m_mapCheckBox[i];//(QCheckBox*)ui.tableWidget->cellWidget(i, j);
				if (checkbox)
				{
					if (checkbox->checkState() == Qt::Checked)
					{
						ItemRows.append("是");
					}
					else
					{
						ItemRows.append("否");
					}
				}
				else
				{
					ItemRows.append(item->text());
				}
			}
			else if (j == 3)
			{
				ItemRows.append(item->text());
			}
		}
		for (int j=0; j<ItemRows.size(); j++)
		{
			if (j == 0)
			{
				str += "| ";
				str += ItemRows[j];
			}
			else if (j == ItemRows.size() - 1)
			{
				str += " | ";
				str += ItemRows[j];
				str += " |";
			}
			else
			{
				str += " | ";
				str += ItemRows[j];
			}
		}
		str += "\n";

	}
	ui.textEdit_Markdown->setText(str);
}
